package com.orangelife.mobile.app.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.baidu.mapapi.SDKInitializer;
import com.common.sp.SharedPreferUtil;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.orangelife.mobile.common.db.DBHelper;
import com.orangelife.mobile.util.LogE;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;
import com.thirdsdk.umeng.UMShare;
import com.umeng.analytics.MobclickAgent;
import com.umeng.common.message.Log;
import com.zhuge.analysis.stat.ZhugeSDK;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by HeYuCan on 2016/8/25.
 */
public class InitAppService extends IntentService {
    public InitAppService( ) {
        this("InitAppService" );
    }
    private boolean isDebugHttp;
    private boolean isDebugSDK;

    public InitAppService(String name) {
        super(name);
    }
    private Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            initHttpUtil();
            initStringUtil();
            initToastHelper();
            initSharePreferenceUtil();
//            setDebug();
            initZhuGe();
            initVolleyV3Helper();
            initImageLoader(getApplicationContext());
            initDBHelper();
                  /*第三方*/
            initUmengShare();// umeng分享
            SDKInitializer.initialize(getApplicationContext()); // 百度地图初始化
            JPushInterface.setDebugMode(false); // 设置开启日志,发布时请关闭日志
            JPushInterface.init(getApplicationContext()); // 初始化 JPush
            SDKInitializer.initialize(getApplicationContext()); // 百度地图初始化

        }
    };

    @Override
    protected  void onHandleIntent(Intent var1){
    mHandler.sendEmptyMessage(0);

    }



    /**
     * 初始化诸葛IO
     */
    private void initZhuGe() {
//        ZhugeSDK.getInstance().openDebug();
//        ZhugeSDK.getInstance().openLog();
        /*参数openGestureBindingUI是可视化事件定义的开关，只有赋值为true时才能连接诸葛io进行
          事件定义。 打开此开关的设备，耗电会有所增加，但不影响应用的稳定。尽管如此，我们还是
          强烈建议您将打开此开关的 应用仅分发给需要的人员，正式分发给用户的版本，请将
          openGestureBindingUI设置为false.*/
        ZhugeSDK.getInstance().codelessInit(this, isDebugSDK);
    }




    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.shop_header) //
                .showImageForEmptyUri(R.drawable.shop_header)
                .showImageOnFail(R.drawable.shop_header)
                .cacheInMemory(true).cacheOnDisk(true).build();

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.defaultDisplayImageOptions(defaultOptions);
        config.threadPriority(Thread.NORM_PRIORITY - 2);//default 设置当前线程的优先级
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB sd卡(本地)缓存的最大值
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        //config.writeDebugLogs(); // Remove for release app
        config.threadPoolSize(5);
        config.memoryCache(new WeakMemoryCache());
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }
    /**
     * 最新的Volley HTTP Util,V3
     */
    private void initVolleyV3Helper() {
        VolleyHttpUtil.getInstance().setServerURL(getServerURL());
        VolleyHttpUtil.getInstance().initVolleyHttpUtil(getApplicationContext());

    }

    private void initToastHelper() {
        ToastHelper.getInstance().init(getApplicationContext());
        com.curry.android.util.ToastHelper.getInstance().init(getApplicationContext());
    }

    private void initSharePreferenceUtil() {
        SharedPreferUtil.getInstance().initSharePrefs(getApplicationContext());
    }

    /**
     * 初始化StringUtil中的上下文
     *
     * @return
     */
    private void initStringUtil() {
        StringUtil.getInstance().init(getApplicationContext());
    }

    /**
     * 旧版的HTTP Util
     */
    private void initHttpUtil() {
        HttpUtil.getHttp().setServerURL(getServerURL());
    }

    public String getServerURL() {
        return getApplicationContext().getString(R.string.server_url);
    }

    private void initDBHelper() {
        DBHelper.init(getApplicationContext());
        DBHelper.getInstance();
    }

    /**
     * umeng
     */
    private void initUmengShare() {
        UMShare.getInstance().init(getApplicationContext());
        MobclickAgent.setCatchUncaughtExceptions(true);
        /*集成测试时用，测试数据将不会被统计到正式后台*/
        //MobclickAgent.setDebugMode(true);
        //UmengUpdateAgent.update(this);
    }

}
